package flc.ast.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.apkextract.lib.model.AeUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ScreenAppAdapter;
import flc.ast.bean.AppScreenBean;
import flc.ast.databinding.ActivityScreenAppBinding;
import flc.ast.dialog.ScreenOriDialog;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class ScreenAppActivity extends BaseAc<ActivityScreenAppBinding> {

    private ScreenAppAdapter mScreenAppAdapter;


    @Override
    protected int onCreate() {
        return R.layout.activity_screen_app;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivScreenAppBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mScreenAppAdapter = new ScreenAppAdapter();
        mDataBinding.rvScreenAppList.setLayoutManager(new LinearLayoutManager(mContext));
        mDataBinding.rvScreenAppList.setAdapter(mScreenAppAdapter);
        mScreenAppAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        showScreenOriDialog(position);
    }

    @Override
    protected void initData() {
        List<AppScreenBean> collectList = AppSpUtils.getAppScreenList();
        if (!CollectionUtils.isEmpty(collectList)) {
            mScreenAppAdapter.setList(collectList);
        } else {
            initAppData();
        }
    }

    private void initAppData() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<AppScreenBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AppScreenBean>> emitter) {
                List<AppScreenBean> beans = new ArrayList<>();
                List<String> packageNames = AeUtil.getNoSystemInstalledPackageNames();
                for (String packName : packageNames) {
                    beans.add(new AppScreenBean(packName, Content.UNDEFINED));
                }
                emitter.onNext(beans);
            }

            @Override
            public void accept(List<AppScreenBean> packageNames) {
                dismissDialog();
                if (!CollectionUtils.isEmpty(packageNames)) {
                    AppSpUtils.saveAppScreenList(packageNames);
                    mScreenAppAdapter.setList(packageNames);
                }
            }
        });
    }

    private void showScreenOriDialog(int pos) {
        ScreenOriDialog dialog = new ScreenOriDialog(mContext);
        dialog.setSelPos(mScreenAppAdapter.getItem(pos).getScreenOrientation());
        dialog.setListener(new ScreenOriDialog.IListener() {
            @Override
            public void clickConfirm(int position) {
                mScreenAppAdapter.getItem(pos).setScreenOrientation(position);
                AppSpUtils.saveAppScreenList(mScreenAppAdapter.getData());
                initData();
                if (SPUtils.getInstance().getBoolean(Content.TOAST_KEY)) {
                    ToastUtils.showShort(R.string.set_success_tips);
                }
            }
        });
        dialog.show();
    }

}